"ui";

var Utils = require('./Utils.js')

var flag = false
var storage = storages.create('yys')
var num = storage.get('num', 2)    // 地域鬼王数量
var level = storage.get('level', 20)    // 地域鬼王等级
var wt_num = storage.get('wt_num', 29)    // 挖土次数
var wt_time = storage.get('wt_time', 38)    // 挖土时间
var bd_api_key = storage.get('bd_api_key', '')   // 百度 api key
var bd_secret_key = storage.get('bd_secret_key', '')   // 百度 secret key
var my = storage.get('my', '')   // 密钥
var wechat, qq   // 微信、QQ
threads.start(function () {
    let data = Utils.getRequest('http://xjj0427.icu/wp-json/xjj/contacts')
    data = data.body.json()
    wechat = data.wechat
    qq = data.qq
})

// 启动阴阳师
function lanchYys(){
    // launchApp('阴阳师')
    // 小米
    // launch('com.netease.onmyoji.mi')
    // IOS
    launch('com.netease.onmyoji.netease_simulator')
    
}

var baseColor = '#4fb3ff'
var white_color = '#ffffff'

ui.statusBarColor(baseColor)  // 设置当前界面的状态栏颜色

ui.layout(
    <frame>
        <vertical>
            <appbar>
                <toolbar title="痒痒鼠" bg="{{baseColor}}" />
            </appbar>
            <card id="dygw" w="*" h="70" margin="10 5" cardCornerRadius="2dp"
                cardElevation="1dp" foreground="?selectableItemBackground">
                <horizontal gravity="center_vertical">
                    <View bg="#f44336" h="*" w="10" />
                    <vertical padding="10 8" h="auto" w="0" layout_weight="1">
                        <text text="地域鬼王" textColor="#222222" textSize="20" maxLines="1" />
                    </vertical>
                </horizontal>
            </card>
            <card id="ltp" w="*" h="70" margin="10 5" cardCornerRadius="2dp"
                cardElevation="1dp" foreground="?selectableItemBackground">
                <horizontal gravity="center_vertical">
                    <View bg="#4caf50" h="*" w="10" />
                    <vertical padding="10 8" h="auto" w="0" layout_weight="1">
                        <text text="寮突破" textColor="#222222" textSize="20" maxLines="1" />
                    </vertical>
                </horizontal>
            </card>
            <card id="wt" w="*" h="70" margin="10 5" cardCornerRadius="2dp"
                cardElevation="1dp" foreground="?selectableItemBackground">
                <horizontal gravity="center_vertical">
                    <View bg="#ff5722" h="*" w="10" />
                    <vertical padding="10 8" h="auto" w="0" layout_weight="1">
                        <text text="挖土" textColor="#222222" textSize="20" maxLines="1" />
                    </vertical>
                </horizontal>
            </card>
            <card id="jjtp" w="*" h="70" margin="10 5" cardCornerRadius="2dp"
                cardElevation="1dp" foreground="?selectableItemBackground">
                <horizontal gravity="center_vertical">
                    <View bg="#2196f3" h="*" w="10" />
                    <vertical padding="10 8" h="auto" w="0" layout_weight="1">
                        <text text="结界突破" textColor="#222222" textSize="20" maxLines="1" />
                    </vertical>
                </horizontal>
            </card>
            <card id="bdocr" w="*" h="70" margin="10 5" cardCornerRadius="2dp"
                cardElevation="1dp" foreground="?selectableItemBackground">
                <horizontal gravity="center_vertical">
                    <View bg="#0aa284" h="*" w="10" />
                    <vertical padding="10 8" h="auto" w="0" layout_weight="1">
                        <text text="百度 OCR" textColor="#222222" textSize="20" maxLines="1" />
                    </vertical>
                </horizontal>
            </card>
            <card id="miyao" w="*" h="70" margin="10 5" cardCornerRadius="2dp"
                cardElevation="1dp" foreground="?selectableItemBackground">
                <horizontal gravity="center_vertical">
                    <View bg="#600aa2" h="*" w="10" />
                    <vertical padding="10 8" h="auto" w="0" layout_weight="1">
                        <text text="密钥" textColor="#222222" textSize="20" maxLines="1" />
                    </vertical>
                </horizontal>
            </card>
            <card id="contact" w="*" h="70" margin="10 5" cardCornerRadius="2dp"
                cardElevation="1dp" foreground="?selectableItemBackground">
                <horizontal gravity="center_vertical">
                    <View bg="#600aa2" h="*" w="10" />
                    <vertical padding="10 8" h="auto" w="0" layout_weight="1">
                        <text text="联系方式" textColor="#222222" textSize="20" maxLines="1" />
                    </vertical>
                </horizontal>
            </card>
        </vertical>
    </frame>
)

// 地域鬼王
ui.dygw.click(view => {
    var dialog = new android.app.AlertDialog.Builder(activity).create()
    dialog.setView(new android.widget.EditText(context))
    dialog.show()
    dialog.setCancelable(false)
    var window = dialog.getWindow()
    window.setDimAmount(0.6)
    var vv = ui.inflate(
        <vertical>
            <vertical margin="25 25 25 10">
                <linear>
                    <text gravity="center" color="#555555" size="17" text="数量 " />
                    <input textColor="#000000" inputType="number" id="num" w="*" text="{{num}}" />
                </linear>
                <linear>
                    <text gravity="center" color="#555555" size="17" text="等级" />
                    <input textColor="#000000" id="level" w="*" text="{{level}}" />
                </linear>
            </vertical>
            <relative>
                <button id="cancle" layout_alignParentLeft="true" text="取消" style="Widget.AppCompat.Button.Borderless.Colored" w="auto" />
                <button id="login" layout_alignParentRight="true" text="开始" style="Widget.AppCompat.Button.Borderless.Colored" w="auto" />
            </relative>
        </vertical>
    )
    window.setContentView(vv)
    vv.cancle.click(() => { dialog.dismiss() })

    vv.login.click(() => {
        let my = storage.get('my')
        !flag && threads.start(function () { flag = Utils.isAlive(my) })
        setTimeout(() => {
            if (flag) {
                if (vv.num.text() > 0 && vv.num.text() < 4) {
                    if (vv.level.text() > 0 && vv.level.text() <= 40) {
                        storage.put('num', vv.num.text())
                        storage.put('level', vv.level.text())
                        lanchYys()
                        engines.execScriptFile("./地域鬼王-yys.js")
                        dialog.dismiss()
                        Utils.stopJB()
                    } else {
                        toast('等级可输入的范围 1 ~ 40 ！')
                        return
                    }
                } else {
                    toast('数量可输入的范围 1 ~ 3 ！')
                    return
                }
            } else {
                dialog.dismiss()
                toastLog('密钥已失效或不存在')
            }
        }, 2000)
    })
})

// 寮突破
ui.ltp.click(view => {
    let my = storage.get('my')
    !flag && threads.start(function () { flag = Utils.isAlive(my) })
    setTimeout(() => {
        if (flag) {
            let ltp_runTime = storage.get('ltp_runTime', '60')
            rawInput("请输入运行时间（分钟）", ltp_runTime).then(time => {
                if (Number(time) > 0) {
                    storage.put('ltp_runTime', time)
                    lanchYys()
                    engines.execScriptFile("./寮突破-yys.js")
                    Utils.stopJB()
                }
                else if (time == null) { }
                else { toast('请输入正确的时间数字！') }
            })
        } else {
            toastLog('密钥已失效或不存在')
        }
    }, 2000)
})

// 挖土
ui.wt.click(view => {
    var dialog = new android.app.AlertDialog.Builder(activity).create()
    dialog.setView(new android.widget.EditText(context))
    dialog.show()
    dialog.setCancelable(false)
    var window = dialog.getWindow()
    window.setDimAmount(0.6)
    var vv = ui.inflate(
        <vertical>
            <vertical margin="25 25 25 10">
                <linear>
                    <text gravity="center" color="#555555" size="17" text="要挖多少次： " />
                    <input textColor="#000000" inputType="number" id="wt_num" w="*" text="{{wt_num}}" />
                </linear>
                <linear>
                    <text gravity="center" color="#555555" size="17" text="挖土时间(秒)：" />
                    <input textColor="#000000" id="wt_time" w="*" text="{{wt_time}}" />
                </linear>
            </vertical>
            <relative>
                <button id="cancle" layout_alignParentLeft="true" text="取消" style="Widget.AppCompat.Button.Borderless.Colored" w="auto" />
                <button id="start" layout_alignParentRight="true" text="开始" style="Widget.AppCompat.Button.Borderless.Colored" w="auto" />
            </relative>
        </vertical>
    )
    window.setContentView(vv)
    vv.cancle.click(() => { dialog.dismiss() })

    vv.start.click(() => {
        let my = storage.get('my')
        !flag && threads.start(function () { flag = Utils.isAlive(my) })
        setTimeout(() => {
            if (flag) {
                if (vv.wt_num.text() > 0) {
                    if (vv.wt_time.text() > 0) {
                        storage.put('wt_num', vv.wt_num.text())
                        storage.put('wt_time', vv.wt_time.text())
                        lanchYys()
                        engines.execScriptFile("./魂十一-yys.js")
                        dialog.dismiss()
                        Utils.stopJB()
                    } else {
                        toast('挖土时间至少是 1 秒！')
                        return
                    }
                } else {
                    toast('挖土次数至少是 1 次！')
                    return
                }
            } else {
                dialog.dismiss()
                toastLog('密钥已失效或不存在')
            }
        }, 2000)
    })
})

// 结界突破
ui.jjtp.click(view => {
    let my = storage.get('my')
    !flag && threads.start(function () { flag = Utils.isAlive(my) })
    setTimeout(() => {
        if (flag) {
            confirm("开始突破？").then(flag => {
                if (flag) {
                    lanchYys()
                    engines.execScriptFile("./结界突破-yys.js")
                    Utils.stopJB()
                }
            })
        } else {
            toastLog('密钥已失效或不存在')
        }
    }, 2000)
})

// 百度 OCR
ui.bdocr.click(view => {
    var dialog = new android.app.AlertDialog.Builder(activity).create()
    dialog.setView(new android.widget.EditText(context))
    dialog.show()
    dialog.setCancelable(false)
    var window = dialog.getWindow()
    window.setDimAmount(0.6)
    var vv = ui.inflate(
        <vertical>
            <vertical margin="25 25 25 10">
                <linear>
                    <text gravity="center" color="#555555" size="17" text="API Key " />
                    <input textColor="#000000" inputType="textVisiblePassword" id="bd_api_key" w="*" text="{{bd_api_key}}" />
                </linear>
                <linear>
                    <text gravity="center" color="#555555" size="17" text="Secret Key" />
                    <input textColor="#000000" inputType="textVisiblePassword" id="bd_secret_key" w="*" text="{{bd_secret_key}}" />
                </linear>
            </vertical>
            <relative>
                <button id="cancle" layout_alignParentLeft="true" text="取消" style="Widget.AppCompat.Button.Borderless.Colored" w="auto" />
                <button id="save" layout_alignParentRight="true" text="保存" style="Widget.AppCompat.Button.Borderless.Colored" w="auto" />
            </relative>
        </vertical>
    )
    window.setContentView(vv)
    vv.cancle.click(() => { dialog.dismiss() })

    vv.save.click(() => {
        bd_api_key = vv.bd_api_key.text()
        bd_secret_key = vv.bd_secret_key.text()
        storage.put('bd_api_key', bd_api_key)
        storage.put('bd_secret_key', bd_secret_key)
        if (storage.get('bd_api_key') === vv.bd_api_key.text() && storage.get('bd_secret_key') === vv.bd_secret_key.text()) {
            toastLog('保存成功')
        } else {
            toastLog('保存失败')
        }
        dialog.dismiss()
    })
})

// 密钥
ui.miyao.click(view => {
    var dialog = new android.app.AlertDialog.Builder(activity).create()
    dialog.setView(new android.widget.EditText(context))
    dialog.show()
    dialog.setCancelable(false)
    var window = dialog.getWindow()
    window.setDimAmount(0.6)
    var vv = ui.inflate(
        <vertical>
            <vertical margin="25 25 25 10">
                <linear>
                    <text gravity="center" color="#555555" size="17" text="密钥" />
                    <input textColor="#000000" inputType="textVisiblePassword" id="my" w="*" text="{{my}}" />
                </linear>
            </vertical>
            <relative>
                <button id="cancle" layout_alignParentLeft="true" text="取消" style="Widget.AppCompat.Button.Borderless.Colored" w="auto" />
                <button id="save" layout_alignParentRight="true" text="保存" style="Widget.AppCompat.Button.Borderless.Colored" w="auto" />
            </relative>
        </vertical>
    )
    window.setContentView(vv)
    vv.cancle.click(() => { dialog.dismiss() })

    vv.save.click(() => {
        my = vv.my.text()
        storage.put('my', my)
        if (storage.get('my') === vv.my.text()) {
            toastLog('密钥保存成功')
        } else {
            toastLog('密钥保存失败')
        }
        dialog.dismiss()
    })
})

// 联系方式
ui.contact.click(view => {
    var dialog = new android.app.AlertDialog.Builder(activity).create()
    dialog.setView(new android.widget.EditText(context))
    dialog.show()
    dialog.setCancelable(false)
    var window = dialog.getWindow()
    window.setDimAmount(0.6)
    var vv = ui.inflate(
        <vertical>
            <vertical margin="25 25 25 10">
                <linear>
                    <text gravity="center" color="#555555" size="17" text="微信：" />
                    <text gravity="center" color="#555555" size="17" text="{{wechat}}" />
                </linear>
                <linear>
                    <text gravity="center" color="#555555" size="17" text="QQ：" />
                    <text gravity="center" color="#555555" size="17" text="{{qq}}" />
                </linear>
            </vertical>
            <relative>
                <button id="cancle" layout_alignParentRight="true" text="取消" style="Widget.AppCompat.Button.Borderless.Colored" w="auto" />
            </relative>
        </vertical>
    )
    window.setContentView(vv)
    vv.cancle.click(() => { dialog.dismiss() })
})
